
let Member = require('./../../model/member')

let create = async (ctx, next) => {
    let body = ctx.request.body;
    console.log(body)
    let auth = ctx.auth;
    let verify = new global.sess.VerifyField([{
        name: 'memberName',
        msg: '队员名字必填'
    }, {
            name: 'memberMobile',
            msg: '队员联系方式必填'
    }, {
        name: 'createrId',
        msg: '创建人ID必填'
        }, {
            name: 'boatId',
            msg: '队伍ID必填'
        }]);
    verify.getField(ctx, next);
    Member.create({
        memberName: body.memberName,
        memberMobile: body.memberMobile,
        memberWeight: body.memberWeight ||'',
        memberHeight: body.memberHeight || '',
        memberRemark: body.memberRemark || '',
        memberJoinTime: body.memberJoinTime || '',
        memberBirthday: body.memberBirthday || '',
        memberLinkman: body.memberLinkman || '',
        memberLinkMobile: body.memberLinkMobile || '',
        memberState: body.memberState || '',
        boatId: body.boatId,
        createrId: auth._id,
        createrMobile: auth.mobile
    });
    ctx.body = new resModel.Success('添加成功')
}

let update = async (ctx, next) => {
    let body = ctx.request.body;
    let auth = ctx.auth;
    if (!body._id) {
        ctx.body = new resModel.def('队员ID必填')
    } else {
        await Memeber.findOneAndUpdate({ _id: mongoose.Types.ObjectId(body._id) }, {
            $set: Obect.assign(body, { updateId: auth._id, updateMobile: auth.mobile })
        }, { new: true }).then(res => {
            if (res) {
                ctx.body = new resModel.Success('修改成功')
            } else {
                ctx.body = new resModel.Delete()
            }
        }).catch(error => {
            ctx.body = new resModel.Server()
        })
    }
}

let remove = async (ctx, next) => {
    let body = ctx.request.body;
    if (!body._id) {
        ctx.body = new resModel.def('队伍ID必填')
    } else {
        await Member.findByIdAndRemove(mongoose.Types.ObjectId(body._id)).then(res => {
            if (res) {
                ctx.body = new resModel.Success('修改成功')
            } else {
                ctx.body = new resModel.Delete()
            }
        }).catch(error => {
            ctx.body = new resModel.Server()
        })
    }
}

let list = async (ctx, next) => {
    // 建议新建者id等信息
    let boatId = ctx.request.body.boatId;
    let cb = await Member.find({ boatId });
    if (!cb) {
        sess.mongoError(ctx, next, error)
    } else {
        ctx.body = new resModel.Success(cb, '查询成功')
    }
}


module.exports = {
    create,
    update,
    remove,
    list
}